<?php
namespace Admin\Controller;

use Think\Controller;

class IndexController extends Controller
{

    /**
     * [主页统计信息展示]
     * @method index
     * @auther 大业
     * @create 2016-10-05
     * @return [type] [description]
     */
    public function index()
    {
        $schID = 1;//$_COKKIE['school_id'];
        $schName= '点点豆豆幼儿园'; //$_COKKIE['school_name'];

        $Env['title'] = '幼儿园信息';
        $Env['name'] = $schName;
        $Env['today'] = date("Y-m-d",time());

        //获取班级编号列表
        $where = 'sch_id = '.$schID;
        $claList = D('ClassToTeacher')->getClaNoList($where);
        //园内人数
        $schNum = $this->getPeopleNum($schID);
        //班级情况
        $claInfo = $this->getClassInfo($schID,$claList);
        //考勤情况
        $attInfo = $this->getAttendRecode($schID,$claList);
        // dump($schNum);
        // dump($claInfo);
        // dump($attInfo);
        // exit;
        $this->assign('Env',$Env);
        $this->assign('schNum',$schNum);
        $this->assign('claInfo',$claInfo);
        $this->assign('attInfo',$attInfo);
        $this->display('Index/index');
    }


    /**
     * [校园人数统计]
     * @method getPeopleNum
     * @auther 大业
     * @create 2016-10-05
     * @param  [int] $schID [学校ID]
     * @return [type] [description]
     */
    private function getPeopleNum($schID)
    {
        if(empty($schID)){
            return null;
        }
        $where = 'sch_id = '.$schID;
        //班级数
        $schNum['cla'] = D('ClassInfo')->getCountNum($where);
        //老师数
        $schNum['tea'] = D('TeacherInfo')->getCountNum($where);
        //学生数
        $schNum['stu'] = D('StudentInfo')->getCountNum($where);
        //园内人数=学生数+老师数
        $schNum['all'] = $schNum['tea']+$schNum['stu'];
        return $schNum;
    }


    /**
     * [班级统计情况]
     * @method getClassInfo
     * @auther 大业
     * @create 2016-10-05
     * @param  [type] $schID [学校ID]
     * @param  [array] $claList [班级编号列表]
     * @return [type] [description]
     */
    private function getClassInfo($schID,$claList)
    {
        if(!empty($claList)){
            $info = $claList;
        }else{
            if(empty($schID)){
                return null;
            }
            $where = 'sch_id = '.$schID;
            $info = D('ClassToTeacher')->getClaNoList($where);
        }
        foreach ($info as $key => $no) {
            //班级名称
            $list[$key]['claName'] = $this->getClaName($no);
            //教师名称
            $list[$key]['teaName'] = $this->getTeaName($no);
            //班级人数
            $list[$key]['claNum'] = $this->getClaNum($no);
            //课程人数
            $list[$key]['couNum'] = $this->getCouNum($no);
        }
        return $list;
    }


    /**
     * [考勤统计情况]
     * @method getClassInfo
     * @auther 大业
     * @create 2016-10-05
     * @param  [int] $schID [学校ID]
     * @param  [array] $claList [班级编号列表]
     * @return [type] [description]
     */
    private function getAttendRecode($schID,$claList)
    {
        if(!empty($claList)){
            $info = $claList;
        }else{
            if(empty($schID)){
                return null;
            }
            $where = 'sch_id = '.$schID;
            $info = D('ClassToTeacher')->getClaNoList($where);
        }
        foreach ($info as $key => $no) {
            //班级名称
            $list[$key]['claName'] = $this->getClaName($no);
            //班级人数
            $list[$key]['claNum'] = $this->getClaNum($no);
            //实到人数
            $list[$key]['claActual'] = $this->getActual($no);
            //未到人数=总人数-实到人数
            $list[$key]['claNoArrival'] = $list[$key]['claNum'] -  $list[$key]['claActual'];
            //请假人数
            $list[$key]['claLeave'] = $this->getLeave($no);
        }
        return $list;
    }


    /**
     * [根据班级编号取班级名称]
     * @method getClaName
     * @auther 大业
     * @create 2016-10-05
     * @param  [type] $claNo [description]
     * @return [type] [description]
     */
    private function getClaName($claNo)
    {
        if(empty($claNo)){
            return null;
        }
        $where = "cla_no = '".$claNo."'";
        $claID = D('ClassToTeacher')->findData($where,'cla_id')['cla_id'];
        $claName = D('ClassInfo')->getNameByID($claID);
        return $claName;
    }


    /**
     * [根据班级编号取教师名称列表]
     * @method getTeaName
     * @auther 大业
     * @create 2016-10-05
     * @param  [int] $claNo [班级编号]
     * @return [array] [教师名+管理标识]
     */
    private function getTeaName($claNo)
    {
        if(empty($claNo)){
            return null;
        }
        $where = "cla_no = '".$claNo."'";
        $teaIdList = D('ClassToTeacher')->selectData($where,'tea_id');
        foreach ($teaIdList as $key => $value) {
            $idList[$key] = $value['tea_id'];
        }
        //转换数组为字符串
        $teaID = implode(',',$idList);
        $teaName = D('TeacherInfo')->getNameByID($teaID);
        //dd($teaName);
        return $teaName;
    }


    /**
     * [根据班级编号统计班级人数]
     * @method getClaNum
     * @auther 大业
     * @create 2016-10-05
     * @param  [int] $claNo [班级编号]
     * @return [int] [班级人数]
     */
    private function getClaNum($claNo)
    {
        if(empty($claNo)){
            return null;
        }
        $where = "cla_no = '".$claNo."'";
        $claNum = D('ClassToStudent')->countData($where);
        return $claNum;
    }


    /**
     * [根据班级编号统计分配课程数]
     * @method getClaNum
     * @auther 大业
     * @create 2016-10-05
     * @param  [int] $claNo [班级编号]
     * @return [int] [课程数]
     */
    private function getCouNum($claNo)
    {
        if(empty($claNo)){
            return null;
        }
        $where = "cla_no = '".$claNo."'";
        $claNum = D('ClassToCourse')->countData($where);
        return $claNum;
    }


    /**
     * [根据班级编号取得实到人数]
     * @method getActual
     * @auther 大业
     * @create 2016-10-05
     * @param  [string] $claNo [班级编号]
     * @return [int] [统计数]
     */
    private function getActual($claNo)
    {
        if(empty($claNo)){
            return null;
        }
        $where = "cla_no = '".$claNo."' and att_status=1";
        //出勤状态:0-正常 1-事假 2-病假 3-旷课 4-其它
        $actualNum = D('AttendRecode')->countData($where);
        return $actualNum;
    }


    /**
     * [根据班级编号取得请假人数]
     * @method getLeave
     * @auther 大业
     * @create 2016-10-05
     * @param  [string] $claNo [班级编号]
     * @return [int] [统计数]
     */
    private function getLeave($claNo)
    {
        if(empty($claNo)){
            return null;
        }
        $where = "cla_no = '".$claNo."' and (att_status=1 or att_status=2)";
        //出勤状态:0-正常 1-事假 2-病假 3-旷课 4-其它
        $leaveNum = D('AttendRecode')->countData($where);
        return $leaveNum;
    }



}
